;(function (window,$) {
    // 创建全选对象
    function CheckAll(options) {
        options.checkAll = this;
        return new CheckAll.prototype.init(options);
    }
    CheckAll.prototype = {
        constructor:CheckAll,
        init:function (options) {
             this.checkNumber = 0;
             this.all(options);
             this.check(options);
        },
        all:function (options) {
            var that = this;
            options.checkAll.change(function () {
                  var $checks = $(options.checksSelector);
                  $checks.each(function (index,value,checks) {
                      value.checked = options.checkAll.get(0).checked;
                      if (value.checked) that.checkNumber++;
                      else that.checkNumber--;
                      if(that.checkNumber >= checks.length ) that.checkNumber =  checks.length;
                      if (that.checkNumber <= 0) that.checkNumber = 0;
                  });
                  $checks.checksVal(options.checkValues)
            })
        },
        check:function (options) {
             var that = this;
             var $checks = $(options.checksSelector);
             var length = $checks.elements.length;
             $checks.click(function () {
                 if (this.checked) that.checkNumber++;
                 else that.checkNumber--;
                 if(that.checkNumber >= length ) that.checkNumber =  length;
                 if (that.checkNumber <= 0) that.checkNumber = 0;
                 if (that.checkNumber === length) options.checkAll.get(0).checked = true;
                 if (that.checkNumber !== length)options.checkAll.get(0).checked = false;
             });
             $checks.checksVal(options.checkValues)
        }
    };
    CheckAll.prototype.init.prototype = CheckAll.prototype;
    // 给Utils进行扩展
    $.fn.extends({checkAll:CheckAll})
})(window,Util);